var nhaccuatui = {
    init: function(){
        nhaccuatui.injectDownloadButton();
    },
    injectDownloadButton: function(){
        var newButton = 
        $('<button rel="nofollow" type="button" title="Download" aria-pressed="false" class="mini-tooltip-top btn-angle"><img class="" src="'  + chrome.extension.getURL('icon19.png')  + '"><span>Download</span></button>');
        newButton.click(function(event){
            if( !nhaccuatui.isLoggedIn() ){
//                console.log("not logged in");
                nhaccuatui.performLogin();
//                console.log("after perform logging in");
//                window.location.reload();
//                return;
            }
            var anchors = [];
            if( window.location.href.match(/nghe\?M=.+/)){ //single song
                anchors.push('<a href="' + window.location.href + '"></a>');
            }else{
                anchors = $('#tab-player-target').find('div.main-col>h2>a');
            }
            
            var html = $('<div id="vndownload-container" style="position:absolute; border: 1px solid #000; padding: 10px;z-index:10000; background: white; width: 95%">Click chuột phải vào các links bên dưới và chọn "Save link as..." để download<ul id="vndownload-links"></ul><a style="float:right;" href="#" onclick="$(\'#vndownload-container\').remove(); return false;">Đóng</a></div>');
            html.css({
                top: (event.clientY - 50),
                left: 10
            });
            
            $(document.body).append(html);
            
            for(var index = 0; index < anchors.length; index++){
//                var song = {};            
                var songId = $(anchors[index]).attr('href').replace(/.*\/nghe\?M=(.+)/gi, "$1");
                        
                var params = {
                    action: 'download',
                    songkey: songId
                };
                $.ajax({
                    type: 'POST',
                    url: 'http://www.nhaccuatui.com/api/song.ashx',
                    data: params,
                    dataType: 'html',
                    success: function(response){
                        var anchor = $(response)[0]; 
                        url = $(anchor).attr('href');
                        html.find('#vndownload-links').append('<li><a href="' + url + '" target="_blank">' + url + '</a></li>');
                    },
                    asycn: true
                });
            };
        });
        $('#tab-player').append(newButton);
    },
    isLoggedIn: function(){
        return $('a[title="Đăng nhập"]').length == 0;
    },
    performLogin: function(){
        var params = {
            username: 'nhaccuatui.com',
            password: 'nhaccuatui.com',
            remember: false,
            callback: 'refreshLogin',
            redirect: 0
        };
        //get the login form
        $.ajax({
            url: 'http://www.nhaccuatui.com/dang_nhap?callback=refreshLogin&redirect=0&&',
            data: params,
            dataType: 'html',
            success: function(response){
                params.__VIEWSTATE = $(response).find('#__VIEWSTATE').val();
                
                $.ajax({
                    type: 'POST',
                    url: 'http://www.nhaccuatui.com/dang_nhap?callback=refreshLogin&redirect=0&&',
                    data: params,
                    dataType: 'json',
                    success: function(r){
                        
                    },
                    async: false,
                    cache: false
                });
            },
            async: false,
            cache: false
        });
    }
    
    
};

nhaccuatui.init();